/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.composites;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.cbf.cam.dice.logic.DiceRoller;
import de.cbf.cam.dsa.battle.ui.listeners.EnterNumbers;

/**
 * @author Nebelritter
 *
 */
public class CompositeBlunderChecks extends CompositeDiceRoll {

	

	/**
	 * @uml.property  name="textResult"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Text textResult;
	/**
	 * @uml.property  name="buttonDiceRoll2D6"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonDiceRoll2D6;
	/**
	 * @uml.property  name="buttonResult2"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult2;
	/**
	 * @uml.property  name="buttonResult35"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult35;
	/**
	 * @uml.property  name="buttonResult68"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult68;
	/**
	 * @uml.property  name="buttonResult910"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult910;
	/**
	 * @uml.property  name="buttonResult11"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult11;
	/**
	 * @uml.property  name="buttonResult12"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonResult12;

	/**
	 * @uml.property  name="roll2D6Listeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> roll2D6Listeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="resultSelectionListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> resultSelectionListeners = new ArrayList<ActionListener>();	
	
	/**
	 * @uml.property  name="controlRollListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> controlRollListeners = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="controlRollModifyListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> controlRollModifyListeners = new ArrayList<ActionListener>();
	
	/**
	 * @uml.property  name="resultSelectionAdvance"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$ResultSelectionAdvanceListener"
	 */
	private ResultSelectionAdvanceListener resultSelectionAdvance = new ResultSelectionAdvanceListener();
	
	/**
	 * @uml.property  name="functional"
	 */
	private boolean functional = false;	
	/**
	 * @uml.property  name="resultModifyFunctionality"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$ResultModificationListener"
	 */
	private ResultModificationListener resultModifyFunctionality = new ResultModificationListener();
	/**
	 * @uml.property  name="roll2D6Functionality"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$Roll2D6Listener"
	 */
	private Roll2D6Listener roll2D6Functionality = new Roll2D6Listener();
	/**
	 * @uml.property  name="cancelFunctionality"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$CancelListener"
	 */
	private CancelListener cancelFunctionality = new CancelListener();
	/**
	 * @uml.property  name="readyFunctionality"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$ReadyListener"
	 */
	private ReadyListener  readyFunctionality  = new ReadyListener();
	/**
	 * @uml.property  name="resultSelectionFunctionality"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositeBlunderChecks$ResultSelectionListener"
	 */
	private ResultSelectionListener resultSelectionFunctionality = new ResultSelectionListener();
	
	/**
	 * @uml.property  name="buttonCancel"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonCancel;
	/**
	 * @uml.property  name="buttonReady"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Button buttonReady;
	/**
	 * @uml.property  name="controlThrowUI"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private CompositeDiceRollCheck controlThrowUI;
	
	
	public CompositeBlunderChecks(Composite parent, int style) {
		super(parent, style);		
	}
	@Override
	protected void buildContent(Composite parent) {
		GridLayout layout = new GridLayout(3, false);
		
		setLayout(layout);
	
		controlThrowUI = new CompositeDiceRollCheck(this, SWT.NONE);		
		controlThrowUI.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false,2,1));
		controlThrowUI.setTitle("Kontrollwurf");
		
		controlThrowUI.registerRollDiceListener(new ControlRollListener());
		controlThrowUI.registerResultModifyListener(new ControlRollModifyListener());
					
		Group groupBlunder = new Group(this, SWT.NONE);
		groupBlunder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false,1,2));
		groupBlunder.setText("Patzerergebnis");
		layout = new GridLayout(2, false);
		layout.marginBottom = 0;
		layout.marginHeight = 0;
		layout.marginLeft 	= 0;
		layout.marginRight	= 0;
		layout.marginTop	= 0;
		layout.marginWidth	= 0;	
		groupBlunder.setLayout(layout);
		
			buttonResult2= new Button(groupBlunder,SWT.RADIO);
			buttonResult2.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult2.setText("2");
			buttonResult2.setData(new Integer(2));
			buttonResult2.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult2 = new Label(groupBlunder, SWT.LEFT);
			labelResult2.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult2.setText("Waffe verloren");
			
			buttonResult35= new Button(groupBlunder,SWT.RADIO);
			buttonResult35.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult35.setText("3-5");
			buttonResult35.setData(new Integer(3));
			buttonResult35.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult35 = new Label(groupBlunder, SWT.LEFT);
			labelResult35.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult35.setText("Sturz");
			
			buttonResult68= new Button(groupBlunder,SWT.RADIO);
			buttonResult68.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult68.setText("6-8");
			buttonResult68.setData(new Integer(6));
			buttonResult68.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult68 = new Label(groupBlunder, SWT.LEFT);
			labelResult68.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult68.setText("Stolpern");
			
			buttonResult910= new Button(groupBlunder,SWT.RADIO);
			buttonResult910.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult910.setText("9-10");
			buttonResult910.setData(new Integer(9));
			buttonResult910.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult910 = new Label(groupBlunder, SWT.LEFT);
			labelResult910.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult910.setText("Sturz");
			
			buttonResult11= new Button(groupBlunder,SWT.RADIO);
			buttonResult11.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult11.setText("11");
			buttonResult11.setData(new Integer(11));
			buttonResult11.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult11 = new Label(groupBlunder, SWT.LEFT);
			labelResult11.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult11.setText("Eigentreffer");
						
			buttonResult12= new Button(groupBlunder,SWT.RADIO);
			buttonResult12.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			buttonResult12.setText("12");
			buttonResult12.setData(new Integer(12));
			buttonResult12.addSelectionListener(resultSelectionAdvance);
			
			Label labelResult12 = new Label(groupBlunder, SWT.LEFT);
			labelResult12.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,false,false,1,1));
			labelResult12.setText("Schwerer Eigentreffer");
		
		buttonDiceRoll2D6 = new Button(this,SWT.PUSH);
		buttonDiceRoll2D6.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		buttonDiceRoll2D6.setText("2W6");
		buttonDiceRoll2D6.addSelectionListener(new Roll2D6AdvanceListener());
				
		textResult = new Text(this, SWT.BORDER|SWT.CENTER);
		textResult.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		textResult.setText("");		
		textResult.setTextLimit(2);
		textResult.addVerifyListener(EnterNumbers.getInstance());
		textResult.addModifyListener(new ResultModificationAdvanceListener());
						
		Composite compositeButtons = new Composite(this, SWT.NONE);
		compositeButtons.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false,3,1));
		compositeButtons.setLayout(new GridLayout(2, false));
		
			buttonCancel= new Button(compositeButtons,SWT.PUSH);
			buttonCancel.setLayoutData(new GridData(SWT.RIGHT,SWT.CENTER,true,false,1,1));
			buttonCancel.setText("Abbrechen");
						
			buttonReady= new Button(compositeButtons,SWT.PUSH);
			buttonReady.setLayoutData(new GridData(SWT.RIGHT,SWT.CENTER,false,false,1,1));
			buttonReady.setText("Fertig");			
		
	}
	
	public int getBlunderValue() {
		Integer blunderValue=0;
		try{
			blunderValue = Integer.parseInt(textResult.getText());
			return blunderValue;
		}catch (NumberFormatException e) {
			return blunderValue;
		}		
		
	}
	public void clearAllSelectedButtons() {
		buttonResult2.setSelection(false);
		buttonResult35.setSelection(false);
		buttonResult68.setSelection(false);;
		buttonResult910.setSelection(false);
		buttonResult11.setSelection(false);
		buttonResult12.setSelection(false);		
	}

	public Integer getControlRoll(){
		return controlThrowUI.getRolls()[0];
	}
	
	public int getControlRollBase() {
		return controlThrowUI.getBase();
	}	
	
	public void setControlRollBase(Integer base){
		controlThrowUI.setBase(base);
	}
	
		
	public void setControlRoll(Integer roll){
		controlThrowUI.setRoll(roll);
	}
	
	public void setControlRollSuccess(boolean success){
		controlThrowUI.setSuccess(success);
	}
	
	public void setControlRollTitle(String title){
		controlThrowUI.setTitle(title);
	}	
	
	/**
	 * @param functional
	 * @uml.property  name="functional"
	 */
	public void setFunctional(boolean functional) {
		if(!this.functional&&functional){
			textResult.addModifyListener(resultModifyFunctionality);
			buttonDiceRoll2D6.addSelectionListener(roll2D6Functionality);
			buttonCancel.addSelectionListener(cancelFunctionality);
			buttonReady.addSelectionListener(readyFunctionality);
			buttonResult2.addSelectionListener(resultSelectionFunctionality);
			buttonResult35.addSelectionListener(resultSelectionFunctionality);
			buttonResult68.addSelectionListener(resultSelectionFunctionality);
			buttonResult910.addSelectionListener(resultSelectionFunctionality);
			buttonResult11.addSelectionListener(resultSelectionFunctionality);
			buttonResult12.addSelectionListener(resultSelectionFunctionality);
			
		}
		if(this.functional&&!functional){
			textResult.removeModifyListener(resultModifyFunctionality);
			buttonDiceRoll2D6.removeSelectionListener(roll2D6Functionality);
			buttonCancel.removeSelectionListener(cancelFunctionality);
			buttonReady.removeSelectionListener(readyFunctionality);
			buttonResult2.removeSelectionListener(resultSelectionFunctionality);
			buttonResult35.removeSelectionListener(resultSelectionFunctionality);
			buttonResult68.removeSelectionListener(resultSelectionFunctionality);
			buttonResult910.removeSelectionListener(resultSelectionFunctionality);
			buttonResult11.removeSelectionListener(resultSelectionFunctionality);
			buttonResult12.removeSelectionListener(resultSelectionFunctionality);
		
		}		
		this.functional = functional;		
	}
//	============================ Dice UI ========================================
	
	
	
// ============================= Listener Registry ==============================	
	public boolean registerRoll2D6Listener(ActionListener listener){
		return roll2D6Listeners.add(listener);
	}
	public boolean deregisterRoll2D6Listener(ActionListener listener){
		return roll2D6Listeners.remove(listener);
	}	
	public boolean registerResultSelectionListener(ActionListener listener){
		return resultSelectionListeners.add(listener);
	}
	public boolean deregisterResultSelectionListener(ActionListener listener){
		return resultSelectionListeners.remove(listener);
	}	
	
//	================================ Dice UI ==================================
	public boolean registerControlRollListener(ActionListener listener){
		return controlRollListeners.add(listener);
	}
	public boolean registerControlRollModifyListener(ActionListener listener){
		return controlRollModifyListeners.add(listener);
	}
	
	public boolean deregisterControlRollListener(ActionListener listener){
		return controlRollListeners.remove(listener);
	}
	public boolean deregisterControllRollModifyListener(ActionListener listener){
		return controlRollModifyListeners.remove(listener);
	}
	
	
	private class ResultSelectionListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			Integer result = (Integer)e.widget.getData();
			textResult.setText(result.toString());
		}		
	}
	
	private class ResultSelectionAdvanceListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeBlunderChecks.this, ActionEvent.ACTION_PERFORMED,"resultSelected");
			for (ActionListener listener : resultSelectionListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	
	private class ResultModificationListener implements ModifyListener{
		@Override
		public void modifyText(ModifyEvent e) {
			try{
				int value =Integer.parseInt(textResult.getText());
				clearAllSelectedButtons();
				switch (value) {
					case 2:buttonResult2.setSelection(true);break;
					case 3:buttonResult35.setSelection(true);break;
					case 4:buttonResult35.setSelection(true);break;
					case 5:buttonResult35.setSelection(true);break;
					case 6:buttonResult68.setSelection(true);break;
					case 7:buttonResult68.setSelection(true);break;
					case 8:buttonResult68.setSelection(true);break;
					case 9:buttonResult910.setSelection(true);break;
					case 10:buttonResult910.setSelection(true);break;
					case 11:buttonResult11.setSelection(true);break;
					case 12:buttonResult12.setSelection(true);break;
				}
			}catch (NumberFormatException exc) {
				clearAllSelectedButtons();
			}			
		}		
	}
	
	private class ResultModificationAdvanceListener implements ModifyListener{
		@Override
		public void modifyText(ModifyEvent e) {
			ActionEvent event = new ActionEvent(CompositeBlunderChecks.this, ActionEvent.ACTION_PERFORMED,"resultModified");
			for (ActionListener listener : resultModifyListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	
	private class Roll2D6Listener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			Integer result =0;
			result+=DiceRoller.rollDice(6);
			result+=DiceRoller.rollDice(6);			
			textResult.setText(result.toString());
		}		
	}
	
	private class Roll2D6AdvanceListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeBlunderChecks.this, ActionEvent.ACTION_PERFORMED,"resultSelected");
			for (ActionListener listener : roll2D6Listeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	
	private class ReadyListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			CompositeBlunderChecks.this.getShell().dispose();
		}		
	}
	private class CancelListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			textResult.setText(0+"");
			CompositeBlunderChecks.this.getShell().dispose();
		}		
	}
	
	private class ControlRollListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(CompositeBlunderChecks.this,ActionEvent.ACTION_PERFORMED, "controllRoll");
			for (ActionListener listener : controlRollListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	private class ControlRollModifyListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(CompositeBlunderChecks.this,ActionEvent.ACTION_PERFORMED, "controllRollModified");
			for (ActionListener listener : controlRollModifyListeners) {
				listener.actionPerformed(event);
			}
			
		}		
	}
	@Override
	public void disable() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void enable() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void disposeSelf() {
		
		
	}

	@Override
	protected List<ActionListener> getAllRegisteredListeners() {
		List<ActionListener> resultList = new ArrayList<ActionListener>();
		resultList.addAll(roll2D6Listeners);
		resultList.addAll(resultSelectionListeners);
		return resultList;
	}

	@Override
	protected PropertyChangeListener getThrowDataPCL() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setModifierValue(int value) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setModifierText(List<String> text) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Integer getResult() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int[] getRolls() {
		// TODO Auto-generated method stub
		return null;
	}

	
}
